<template>
    <div class='approval'>
      <el-card class="box-card">
        <div class='title'>
          {{$t('achievements.title1')}}
        </div>
        <div class='menu-search'> 
            <div class='menu-title'>
                <!-- APP渠道 -->
                <div class='searchItem'>
                    <span>{{$t('ElectricChannel.AppChannel')}}</span>
                    <el-select v-model='listQuery.channel' size='small'>
                          <el-option
                          v-for='item in appChannel'
                          :key='item.channel'
                          :label='item.org_name'
                          :value='item.channel'>
                          </el-option>
                      </el-select>
                </div>
            </div>
            <div class='menu-title-btn'>
                <button type='button' @click='search'>
                    {{$t('btn.query')}}
                </button>
                <button type='button' @click='reset'>
                    {{$t('btn.reset')}}
                </button>
            </div>
        </div>
      </el-card>
        <el-card class='box-card' style="margin-top: 10px;margin-bottom: 20px;">
          <div class='title'>
              {{$t('approval.title2')}}
          </div>
          <div class='menu-list'>
              <button type='button' @click='createFn'>{{$t('btn.add')}}</button>
          </div>
          <el-table
              v-loading="listLoading"
              ref='multipleTable'
              :data='tableData'
              tooltip-effect='dark'
              border
              size='mini'
              :class="['configurationTable', {afterRenderClass: showAfterRenderClass}]">
              <!-- 原始渠道 -->
              <el-table-column
              header-align='center'
              align='center'
              :label="$t('ElectricChannel.OriginalChannel')">
              <template slot-scope="scope">
                  <span style="color: rgb(0, 0, 204);">{{scope.row.appUrl}}</span>
              </template>
              </el-table-column>
              <!-- APP名称 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='appName'
              :label="$t('appHelpCenter.appName')"
              width="200">
              </el-table-column>
              <!-- APP包名 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='appPackageName'
              :label="$t('AppUpgrade.appName')"
              width="200">
              </el-table-column>
               <el-table-column 
                align="center" 
                :label="$t('user.operation')" 
                width="120"> 
                    <template slot-scope="scope">
                        <el-button size="small" type="danger" @click="handleDelete(scope.row)">{{$t('btn.delete')}}
                        </el-button>
                    </template> 
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="listQuery.page"
            :page-sizes="[10, 30, 50, 100]"
            :page-size="listQuery.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
            </el-pagination>
          </div>
        </el-card>
        <el-dialog :title="$t('btn.add')" :visible.sync="dialogFormVisible" :before-close = "closeDialog">
            <el-form :model="form" :rules="rules" ref="form" label-width="100px" style="width: 90%;">
                <!-- APP渠道 -->
                <el-form-item :label="$t('ElectricChannel.AppChannel')" prop="channel">
                    <el-select v-model="form.channel" :placeholder="$t('rule.choose')" size='small'>
                        <el-option
                          v-for='item in appChannel'
                          :key='item.channel'
                          :label='item.org_name'
                          :value='item.channel'>
                          </el-option>
                    </el-select>
                </el-form-item> 
                <!-- APP名称 -->
                <el-form-item :label="$t('appHelpCenter.appName')" prop="appName">
                    <el-input size='small' v-model="form.appName"></el-input>
                </el-form-item>
                <!-- APP包名 -->
                <el-form-item :label="$t('AppUpgrade.appName')" prop="appPackageName">
                    <el-input size='small' v-model="form.appPackageName"></el-input>
                </el-form-item>
                <!-- 原始链接 -->
                <el-form-item :label="$t('ElectricChannel.OriginalUrl')" prop="appUrl">
                    <el-input size='small' v-model="form.appUrl"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel('form')">{{$t('btn.cancel')}}</el-button>
                <el-button type="primary" @click="create('form')">{{$t('btn.confirm')}}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getChannels } from "api/customQuery/index";
import { loanAppUrlPage, loanAppUrlSave, loanAppUrlDelete } from "api/channelManager/index";
export default {
  data() {
    return {
      showAfterRenderClass: false,
      form: {
        channel: '',
        appName: '',
        appPackageName: '',
        appUrl: ''
      },
      rules: {
        channel: [
          {
            required: true,
            message: this.$t("ElectricChannel.ruleAppChannel"),
            trigger: "change"
          }
        ],
        appName: [
          {
            required: true,
            message: this.$t("creditSupermarket.ruleName"),
            trigger: "blur"
          }
        ],
        appPackageName: [
          {
            required: true,
            message: this.$t("ElectricChannel.ruleAppName"),
            trigger: "blur"
          }
        ],
        appUrl: [
          {
            required: true,
            message: this.$t("ElectricChannel.ruleOriginalUrl"),
            trigger: "blur"
          }
        ]
      },
      dialogFormVisible: false,
      listLoading: false,
      total: null,
      listQuery: {
        limit: 10,
        page: 1,
        channel: ""
      },
      tableData: [],
      appChannel: []
    };
  },
  created() {
    if (this.channelConfigPage.has(this.$route.path)) {
      this.listQuery = this.channelConfigPage.get(this.$route.path);
    }
    // 渠道下拉
    getChannels().then(res => {
      this.appChannel = res.data;
      this.listQuery.channel = this.appChannel[0].channel;
      this.getList();
    });
  },
  computed: {
    ...mapGetters(["elements", "channelConfigPage"])
  },
  updated() {
    const handleScrollX = setInterval(() => {
      if (this.$refs.configurationTable) {
        this.showAfterRenderClass = true;
        clearInterval(handleScrollX);
      }
    }, 500);
  },
  methods: {
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    //重置
    reset() {
      this.listQuery.limit = 10;
      this.listQuery.page = 1;
      this.listQuery.channel = this.appChannel[0].channel;
    },
    closeDialog(done) {
      done();
      this.$refs["form"].resetFields();
      this.getList();
    },
    cancel(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    // 添加保存按钮
    create(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.$confirm(this.$t("loanDetail.cue1"), {
            confirmButtonText: this.$t("btn.confirm"),
            cancelButtonText: this.$t("btn.cancel"),
            type: "warning",
            center: true
          })
            .then(() => {
              loanAppUrlSave(this.form)
              .then(res => {
                if (res.status === 200) {
                  this.dialogFormVisible = false;
                  this.getList();
                  this.$notify({
                    title: this.$t("user.success"),
                    message: this.$t("user.crtsuccess"),
                    type: "success",
                    duration: 2000
                  });
                } else {
                  this.$message({
                    showClose: true,
                    type: "warning",
                    message: res.message
                  });
                }
              });
            })
            .catch(() => {
              this.$message({
                showClose: true,
                type: "info",
                message: this.$t("loanDetail.cue3")
              });
            });
        } else {
          return false;
        }
      });
    },
    // 添加
    createFn() {
      this.form = {
        channel: '',
        appName: '',
        appPackageName: '',
        appUrl: ''
      };
      this.dialogFormVisible = true;
    },
    getList() {
      this.listLoading = true;
      this.$store.dispatch("saveChannelConfigPage", {
        path: this.$route.path,
        listQuery: this.listQuery
      });
      loanAppUrlPage(this.listQuery).then(response => {
        if (response.status === 200) {
          this.listLoading = false;
          this.tableData = response.data.rows;
          this.total = Number(response.data.total);
        }
      });
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    // 点击删除按钮
    handleDelete(row) {
      this.$confirm(this.$t("user.cue1"), this.$t("user.cue2"), {
        confirmButtonText: this.$t("btn.confirm"),
        cancelButtonText: this.$t("btn.cancel"),
        type: "warning"
      })
        .then(() => {
          let obj = {};
          obj.appUrlId = row.id;
          loanAppUrlDelete(obj).then((res) => {
            if (res.status === 200) {
              this.$notify({
                title: this.$t("user.success"),
                message: this.$t("user.delsuccess"),
                type: "success",
                duration: 2000
              });
              const index = this.tableData.indexOf(row);
              this.tableData.splice(index, 1);
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            showClose: true,
            type: "info",
            message: this.$t("orderQuery.cue2")
          });
        });
    }
  }
};
</script>
<style>
.configurationTable .el-table__body-wrapper {
  overflow: hidden;
}
.afterRenderClass .el-table__body-wrapper {
  overflow: auto;
}
</style>

<style scoped lang='scss'>
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before {
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 240px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px 0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
      width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
</style>